package com.hzvdvmc.autosale.setting.ui;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.common.OnItemClickListener;
import com.hzvdvmc.autosale.sale.ui.InputDialog;
import com.hzvdvmc.autosale.setting.adapter.CabinetRowAdapter;
import com.hzvdvmc.autosale.setting.adapter.SimpleTextSpinnerAdatper;
import com.hzvdvmc.autosale.setting.bean.TuneSetBean;
import com.hzvdvmc.autosale.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TuneSetFragment extends Fragment {


    @BindView(R.id.spinner_row)
    Spinner spinner;
    @BindView(R.id.rv_detail)
    RecyclerView rvDetail;
    Unbinder unbinder;

    SpinnerAdapter mSpinnerAdapter;

    private List<String> spinnerDataList = new ArrayList<>();
    private List<TuneSetBean.Cabinet.shopRow> shopRowList;
    private CabinetRowAdapter mAdapter;
    private TuneSetBean.Cabinet cabinet;

    public static TuneSetFragment newInstance(TuneSetBean.Cabinet cabinet) {
        Bundle args = new Bundle();
        args.putSerializable("cabinet", cabinet);
        TuneSetFragment fragment = new TuneSetFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View contentView = inflater.inflate(R.layout.fragment_tune_set, container, false);
        unbinder = ButterKnife.bind(this, contentView);
        cabinet = (TuneSetBean.Cabinet) getArguments().getSerializable("cabinet");
        if (cabinet != null) {
            shopRowList = cabinet.getDataList();
        }
        initSpinnerDataList();
        initSpinner();
        initRecyclerView();
        return contentView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initSpinnerDataList() {
        spinnerDataList.clear();
        for (int i = 1; i <= 6; i++) {
            spinnerDataList.add(i + "");
        }
    }

    private void initSpinner() {
        mSpinnerAdapter = new SimpleTextSpinnerAdatper(spinnerDataList);
        spinner.setAdapter(mSpinnerAdapter);
        spinner.setPrompt("请选择货盘数量");

        if (shopRowList != null && shopRowList.isEmpty()) {
            spinner.setSelection(0);
        } else {
            spinner.setSelection(shopRowList.size() - 1);
        }
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (shopRowList.size() == Integer.parseInt(spinnerDataList.get(position))) {
                    return;
                }
                initRvDataList(Integer.parseInt(spinnerDataList.get(position)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initRvDataList(int num) {
        shopRowList.clear();
        for (int i = 0; i < num; i++) {
            shopRowList.add(new TuneSetBean.Cabinet.shopRow());
        }
        LogUtil.i("修改后：" + shopRowList.toString());
        mAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView() {
        mAdapter = new CabinetRowAdapter(shopRowList);
        rvDetail.setLayoutManager(new LinearLayoutManager(getContext()));
        rvDetail.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                showListDialog(position);
            }
        });
    }

    private void showListDialog(final int position) {
        final String[] items = new String[20];

        for (int i = 0; i < items.length; i++) {
            items[i] = (i + 1) + "";
        }

        int checkedId = Integer.parseInt(shopRowList.get(position).getTuneCount()) - 1;
        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getContext(), R.layout.item_list_dialog_tune, items);

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("请选择 货盘" + (position + 1) + " 的货道数")
                .setCancelable(true)
                .setSingleChoiceItems(arrayAdapter, checkedId, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String count = items[which];
                        shopRowList.get(position).setTuneCount(count);
                        dialog.dismiss();
                        mAdapter.notifyDataSetChanged();
                    }
                })
                .show();
    }

    private void showInputDialog(int inputType, String title, final int position) {
        final InputDialog dialog = new InputDialog(getContext(), 0, inputType);
        dialog.show();
        dialog.setCurrentValue(shopRowList.get(position).getTuneCount());
        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputContent = dialog.getInputContent();
                shopRowList.get(position).setTuneCount(inputContent);
                dialog.dismiss();
                mAdapter.notifyDataSetChanged();
            }
        });
        dialog.setCancelOnClickListenre(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setTitle(title);
        dialog.setEditLength(2);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
